package service.impl;

import dao.CompanyDao;
import domain.Company;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.CompanyService;
import service.ServiceResponse;

/**
 * Created by liuchang on 2015/6/24.
 */
@Service
public class CompanyServiceImpl extends ServiceBase implements CompanyService {

    private Logger logger = LoggerFactory.getLogger(CompanyServiceImpl.class);

    @Autowired
    private CompanyDao companyDao;

    @Override
    public ServiceResponse update(Company company) {
        Company db = companyDao.findByOwner(company.getOwner());

        String province = company.getProvince();
        if (StringUtils.isNotBlank(province)) {
            db.setProvince(province);
        }
        String city = company.getCity();
        if (StringUtils.isNotBlank(city)) {
            db.setCity(city);
        }
        String district = company.getDistrict();
        if (StringUtils.isNotBlank(district)) {
            db.setDistrict(district);
        }
        String address = company.getAddress();
        if (StringUtils.isNotBlank(address)) {
            db.setAddress(address);
        }
        String description = company.getDescription();
        if (StringUtils.isBlank(description)) {
            db.setDescription("");
        }
        String kind = company.getTradeCategory();
        if (StringUtils.isNotBlank(kind)) {
            db.setTradeCategory(kind);
        }
        Double latitude = company.getLatitude();
        if (latitude != null) {
            db.setLatitude(latitude);
        }
        Double longitude = company.getLongitude();
        if (longitude != null) {
            db.setLongitude(longitude);
        }
        String name = company.getName();
        if (StringUtils.isNotBlank(name)) {
            db.setName(name);
        }

        companyDao.update(company);
        return ServiceResponse.success();
    }

    @Override
    public ServiceResponse getByOwner(long userid) {
        Company company = companyDao.findByOwner(userid);
        return ServiceResponse.success().with("company", company);
    }

}
